package com.example.springbootsecurityjwt.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springbootsecurityjwt.domain.User;
import com.example.springbootsecurityjwt.mapper.UserMapper;
import com.example.springbootsecurityjwt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.function.Function;

/**
 * @program: SpringBoot-Security-JWT
 * @description: 用户Service实现类
 * @author: Coder_Pan
 * @create: 2022-03-22 10:53
 **/
@Service
public class UserServiceImpl implements UserService {
    final
    UserMapper userMapper;


    public UserServiceImpl( UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int addUser(String username, String password) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        UUID uuid = new UUID(36,8);
        User user = new User();
        //如果用户名为空，为其设置默认用户名
        if (StringUtils.isBlank(username)) {
            user.setUserName("panpan"+new Random().nextInt(100));
        }else {
            user.setUserName(username);
        }
        //加密后的密码
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);
        user.setUserUuid(uuid.toString());
        user.setNickName("夜半"+new Random().nextInt(100));
        user.setPhonenumber("18866669999");


        return userMapper.insert(user);
    }


}
